home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-06-05 | 9.0 KB | 262 lines | [TEXT/MPS ] |
- {
- File: SamplePascalExtension.p
-
- Contains: Sample Pascal (how quaint!) file translation extension
-
- Copyright: © 1991-1993 by Apple Computer, Inc., all rights reserved.
- }
-
- UNIT SampleExtension;
-
- INTERFACE
-
- USES
- MemTypes, Memory, Resources, Files, Errors, Components, TranslationExtensions;
-
- FUNCTION TranslateEntry( VAR params: ComponentParameters; storage: Handle ) : ComponentResult;
-
- IMPLEMENTATION
-
- CONST
- rProgressAdvertismentResID = 150;
-
- FUNCTION DoGetFileTranslationList(self : ComponentInstance;
- translationList : FileTranslationListHandle):ComponentResult; FORWARD;
-
- FUNCTION DoIdentifyFile(self : ComponentInstance;
- targetDocument : FSSpec;
- VAR documentIdentfiedAs : FileType) :ComponentResult; FORWARD;
-
- FUNCTION DoTranslateFile(self : ComponentInstance;
- refNum : TranslationRefNum;
- sourceDocument : FSSpec;
- sourceDocumentType : FileType;
- sourceDocumentTypeHint : LONGINT;
- destinationDocument : FSSpec;
- destinationDocumentType : FileType;
- destinationDocumentTypeHint : LONGINT): ComponentResult; FORWARD;
-
-
- {_______________________________________________________________________________________________}
- { }
- { TranslateEntry }
- { }
- { This is the main entry point for the extension. It handles some Component Manager setup, but }
- { it's main task is to dispatch to the extensions subroutines when known selectors are }
- { encountered. }
- { }
- FUNCTION TranslateEntry( VAR params: ComponentParameters; storage: Handle ) :ComponentResult;
- TYPE
- LongHandle = ^LongPtr;
- LongPtr = ^Longint;
- VAR
- self: ComponentInstance;
- h: Handle;
- selector: INTEGER;
- BEGIN
-
- CASE (params.what) OF
-
- { Handle the Component Manager opening up this component }
- kComponentOpenSelect:
- BEGIN
- self := ComponentInstance(params.params[0]);
- h := NewHandle(sizeof(ComponentInstance)); { allocate storage }
- IF h <> nil THEN
- BEGIN
- LongHandle(h)^^ := ORD4(self); { put instance in storage }
- SetComponentInstanceStorage(self, h);
- TranslateEntry := noErr;
- END
- ELSE
- TranslateEntry := MemError;
- END;
-
- { Compoenent Manager is closing us down, clean up }
- kComponentCloseSelect:
- BEGIN
- DisposeHandle(storage);
- TranslateEntry := noErr;
- END;
-
- { Compoenent Manager needs to know what selectors we can handle }
- kComponentCanDoSelect:
- BEGIN
- selector := INTEGER((Ptr(params.params)^));
-
- IF ( ((kComponentVersionSelect <= selector) AND (selector <= kComponentOpenSelect))
- OR ((kTranslateGetFileTranslationList <= selector) AND (selector <= kTranslateTranslateFile)) ) THEN
- TranslateEntry := 1
- ELSE
- TranslateEntry := 0;
- END;
-
- { No version stuff }
- kComponentVersionSelect:
- TranslateEntry := noErr;
-
- { Want file translation list? }
- kTranslateGetFileTranslationList:
- TranslateEntry := CallComponentFunctionWithStorage(Handle(storage^^), params, ComponentFunction(@DoGetFileTranslationList));
-
- { Want to identify file? }
- kTranslateIdentifyFile:
- TranslateEntry := CallComponentFunctionWithStorage(Handle(storage^^), params, ComponentFunction(@DoIdentifyFile));
-
- { Want to translate file? }
- kTranslateTranslateFile:
- TranslateEntry := CallComponentFunctionWithStorage(Handle(storage^^), params, ComponentFunction(@DoTranslateFile));
-
- { No, idea, tell the Component Manager that }
- OTHERWISE
- TranslateEntry := badComponentSelector;
- END; { CASE }
- END;
-
-
-
-
- {_______________________________________________________________________________________________}
- { }
- { DoGetFileTranslationList }
- { }
- { This routine fills out a file translation list for this extension. Let's assume that this }
- { extension can do the following translations: }
- { }
- { FROM: TO: }
- { _________________ ________________ }
- { SleepyWrite (slpy) TeachText(ttro) }
- { HappyWrite (hapy) }
- { }
- FUNCTION DoGetFileTranslationList(self : ComponentInstance;
- translationList : FileTranslationListHandle) : ComponentResult;
- CONST
- kStamp = $A74520A8; {time this code was compiled}
- TYPE
- MyList = RECORD
- modDate: LONGINT;
- groupCount: LONGINT;
- group1SrcCount: LONGINT;
- group1SrcEntrySize: LONGINT;
- group1SrcTypes: ARRAY[1..2] OF FileTypeSpec;
- group1DstCount: LONGINT;
- group1DstEntrySize: LONGINT;
- group1DstTypes: ARRAY[1..1] OF FileTypeSpec;
- END;
- MyListPtr = ^MyList;
- MyListHandle = ^MyListPtr;
- VAR
- result : OSErr;
- listPtr : MyListPtr;
- BEGIN
- result := noErr;
-
- { This extension has a "hard-coded" list of translations - it never changes. The first }
- { time Gremlin EVER calls this extension fill out the list, then every other time it }
- { passes us the list back, just pass it back to 'em without looking - things haven't }
- { changed. }
- IF translationList^^.modDate <> kStamp THEN
- BEGIN
- { Adjust the handle so there's enough room for all the data we're going to stuff }
- SetHandleSize( Handle(translationList), sizeof(MyList) );
- result := MemError;
- IF result = noErr THEN
- BEGIN
- listPtr := MyListHandle(translationList)^;
- WITH listPtr^ DO
- BEGIN
- { See we know we've done this before }
- modDate := kStamp;
-
- { We've got one group }
- groupCount := 1;
-
- { src side has two entries }
- group1SrcCount := 2;
- group1SrcEntrySize := SizeOf(FileTypeSpec);
- group1SrcTypes[1].format := 'slpy'; { SleepyWrite document format }
- group1SrcTypes[1].hint := 0; { No hint }
- group1SrcTypes[1].flags := 0; { No specials }
- group1SrcTypes[1].catInfoType := 'slpy'; { Catalog type }
- group1SrcTypes[1].catInfoCreator := 'dsny'; { Catalog creator }
- group1SrcTypes[2].format := 'hapy'; { HappyWrite document format }
- group1SrcTypes[2].hint := 0; { No hint }
- group1SrcTypes[2].flags := 0; { No specials }
- group1SrcTypes[2].catInfoType := 'hapy'; { Catalog type }
- group1SrcTypes[2].catInfoCreator := 'mcky'; { Catalog creator }
-
- { dst side has one entry }
- group1DstCount := 1;
- group1DstEntrySize := SizeOf(FileTypeSpec);
- group1DstTypes[1].format := 'ttro'; { TeachText document format }
- group1DstTypes[1].hint := 0; { No hint }
- group1DstTypes[1].flags := taDstDocNeedsResourceFork; { TeachText documents have resource forks where they store they're pictures }
- group1DstTypes[1].catInfoType := 'ttro'; { Catalog type }
- group1DstTypes[1].catInfoCreator := 'ttxt'; { Catalog creator }
- END ; {with}
- END; {if}
- END; {if}
-
- DoGetFileTranslationList := result;
- END;
-
- {_______________________________________________________________________________________________}
- { }
- { DoIdentifyFile }
- { }
- { This routine is responsible for identifing a file. The actual identification code is not }
- { here, just a shell (sorry). }
- { }
- FUNCTION DoIdentifyFile(self : ComponentInstance;
- targetDocument : FSSpec;
- VAR documentIdentfiedAs : FileType) :ComponentResult;
- VAR
- identified : Boolean;
- BEGIN
- { Use our mystery routine to identify the file }
- { identified := IdentifyDocument(targetDocument, documentIdentfiedAs); }
-
- IF identified THEN
- DoIdentifyFile := noErr
- ELSE
- DoIdentifyFile := noTypeErr;
- END;
-
-
- {_______________________________________________________________________________________________}
- { }
- { DoTranslateFile }
- { }
- { This routine is responsible for tranlating a file. The actual identification code is not }
- { here, just a shell (sorry). }
- { }
- FUNCTION DoTranslateFile(self : ComponentInstance;
- refNum : TranslationRefNum;
- sourceDocument : FSSpec;
- sourceDocumentType : FileType;
- sourceDocumentTypeHint : LONGINT;
- destinationDocument : FSSpec;
- destinationDocumentType : FileType;
- destinationDocumentTypeHint : LONGINT): ComponentResult;
- VAR
- advert: Handle;
- myResFile: INTEGER;
- result: OSErr;
- BEGIN
- { first thing to do is display progress dialog and show advertisement }
- myResFile := OpenComponentResFile(Component(self));
-
- IF (myResFile <> -1) THEN
- BEGIN
- advert := GetResource('PICT', rProgressAdvertismentResID);
- DetachResource(advert);
- result := SetTranslationAdvertisement(refNum, PicHandle(advert));
- result := CloseComponentResFile(myResFile);
- END;
-
- { Now call the magic routine to translate the file }
- { DoTranslateFile := PerformTranslation(refNum, sourceDocument, sourceDocumentType, destinationDocument,destinationDocumentType);}
- END;
-
-
- END.